package com.shuyuntu.proposal.service;

import com.shuyuntu.proposal.dao.IDelegateDao;
import com.shuyuntu.proposal.dao.IMessageDao;
import com.shuyuntu.proposal.entity.Delegate;
import com.shuyuntu.proposal.entity.Message;
import com.shuyuntu.proposal.enums.EMessageType;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by limeng on 16-4-26.
 */
@Service
public class MessageService {
    /**
     * 根据id获得所有未读信息条目
     * @param userId 教师代表id
     * @return 未读信息条目
     */
    @Transactional
    public Integer getUnreadMessagesCount(String userId) {
        Criterion ct1 = Restrictions.eq("receiver.id", userId);
        Criterion ct2 = Restrictions.eq("unread", true);

        long count = messageDao.queryCount(ct1, ct2);
        return new Long(count).intValue();
    }

    public List<Message> getUnreadMessagesList(String userId, int count) {
        Criterion ct1 = Restrictions.eq("receiver.id", userId);
        Criterion ct2 = Restrictions.eq("unread", true);

        List<Message> messages = messageDao.queryPageList(1, count, null, null, ct1, ct2);
        return messages;
    }

    /**
     * 根据用户id获得所有消息条目
     * @param userId 教师代表id
     * @return 消息条数
     */
    @Transactional
    public Integer getMessagesCount(String userId) {
        Criterion ct1 = Restrictions.eq("receiver.id", userId);

        long count = messageDao.queryCount(ct1);
        return new Long(count).intValue();
    }

    /**
     * 分页获取消息
     * @param pageSize 分页大小
     * @param pageNo 页数
     * @param userId 教师代表id
     * @return 消息列表
     */
    @Transactional
    public List<Message> getMessagesWithPagination(int pageSize, int pageNo, String userId) {
        Criterion ct1 = Restrictions.eq("receiver.id", userId);
        Order order = Order.desc("id");
        List<Message> list = messageDao.queryPageList(pageNo, pageSize, null, order, ct1);

        return list;
    }

    @Transactional
    public boolean insertMessage(Message message) {
        try {
            message.setUnread(true);
            messageDao.insert(message);
        } catch (Exception e) {
            return false;
        }

        return true;
    }

    @Transactional
    public boolean sendMessageToUser(String receiverId, String messageContent, boolean type) {
        Message message = new Message();
        Delegate receiver = delegateDao.queryById(receiverId);
        message.setUnread(true);
        message.setReceiver(receiver);
        message.setContent(messageContent);
        message.setSendTime(new Date());
        message.setType(type ? EMessageType.NOTIFICATION : EMessageType.NEWS);

        return insertMessage(message);
    }

    @Transactional
    public int setRead(Integer messageId, String loginUserId) {
        try {
            Message message = messageDao.queryById(messageId);
            Delegate delegate = delegateDao.queryById(loginUserId);
            if (message == null) {
                return 1;
            }
            if (!message.getReceiver().getId().equals(delegate.getId())) {
                return 2;
            }
            message.setUnread(false);
            messageDao.update(message);
            return 0;
        } catch (Exception e) {
            return -1;
        }
    }

    @Resource
    IMessageDao messageDao;

    public IMessageDao getMessageDao() {
        return messageDao;
    }

    public void setMessageDao(IMessageDao messageDao) {
        this.messageDao = messageDao;
    }

    @Resource
    IDelegateDao delegateDao;

    public IDelegateDao getDelegateDao() {
        return delegateDao;
    }

    public void setDelegateDao(IDelegateDao delegateDao) {
        this.delegateDao = delegateDao;
    }

}
